<?php

declare(strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\business;

use App\Dao\CouponCategoryDao;
use App\Middleware\User\UserMiddleware;
use App\Request\Business\Coupon\AddCouponStockRequest;
use App\Request\Business\Coupon\AddShopDiscountRequest;
use App\Request\Business\Coupon\AddVoucherRequest;
use App\Request\Business\Coupon\CheckCouponStockRequest;
use App\Request\Business\Coupon\GenStockValueRequest;
use App\Request\Business\Coupon\DeleteCouponRequest;
use App\Request\Business\Coupon\GetBusinessCouponListRequest;
use App\Request\Business\Coupon\GetCategoryRequest;
use App\Request\Business\Coupon\GetCouponRequest;
use App\Request\Business\Coupon\AddCouponRequest;
use App\Service\Business\CouponService;
use App\Request\Business\Coupon\GetCouponByIdRequest;
use App\Request\Business\Coupon\GetCouponByTypeRequest;
use App\Request\Business\Coupon\GetCouponByOrderNoRequest;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;

/**
 * 优惠券控制器
 *
 * @AutoController()
 * @Middleware(UserMiddleware::class)
 * @author 李想(928674263@qq.com)
 * @package App\Controller\business
 */
class CouponController extends Controller
{
    /**
     * 引入优惠券分类DAO
     *
     * @Inject
     * @var CouponCategoryDao
     */
    private $category_dao;

    /**
     * 引入优惠券服务
     *
     * @Inject
     * @var CouponService
     */
    private $service;
    
    /**
     * 获取分类列表
     *
     * @param GetCategoryRequest $request
     */
    public function get_category(GetCategoryRequest $request)
    {
        $params = $request->all();

        isset($params['type']) && $params['type'] = (int)$params['type'];

        $result = $this->category_dao->getCategoryList(['type' => $params['type']]);
        
        $result->makeHidden(['type', 'home_show', 'sort', 'icon']);

        $this->success($result);
    }

    /**
     * 添加优惠券
     *
     * @param AddCouponRequest $request
     */
    public function add_coupon(AddCouponRequest $request)
    {
        $params = $request->all();

        isset($params['title']) && $params['title'] = (string)$params['title'];
        isset($params['description']) && $params['description'] = (string)$params['description'];
        isset($params['start_time']) && $params['start_time'] = (string)$params['start_time'];
        isset($params['expire_time']) && $params['expire_time'] = (string)$params['expire_time'];
        isset($params['goods_price']) && $params['goods_price'] = (float)$params['goods_price'];
        isset($params['price']) && $params['price'] = (float)$params['price'];
        isset($params['stock']) && $params['stock'] = (int)$params['stock'];
        isset($params['picture']) && $params['picture'] = (string)$params['picture'];
        isset($params['instructions']) && $params['instructions'] = (string)$params['instructions'];

        $this->service->addCoupon($params);

        $this->success();
    }

    /**
     * 获取商家的优惠券列表
     *
     */
    public function get_business_coupon_list()
    {

        $result = $this->service->getBusinessCouponList();

        $this->success($result);
    }

    /**
     * 删除优惠券
     *
     * @param DeleteCouponRequest $request
     */
    public function delete_coupon(DeleteCouponRequest $request)
    {
        $params = $request->all();

        isset($params['ids']) && $params['ids'] = (array)$params['ids'];

        $this->service->deleteCoupon($params['ids']);

        $this->success();
    }

    /**
     * 生成券核销码
     *
     * @param GenStockValueRequest $request
     */
    public function gen_stock_value(GenStockValueRequest $request)
    {
        $params = $request->all();

        isset($params['num']) && $params['num'] = (int)$params['num'];

        $container = ApplicationContext::getContainer();
        $generator = $container->get(IdGeneratorInterface::class);

        $values = [];
        for ($i = 1; $i <= $params['num']; $i++) {
            $values[] = $generator->generate();
        }

        $this->success($values);
    }

    /**
     * 添加优惠券库存
     *
     * @param AddCouponStockRequest $request
     * @throws
     */
    public function add_coupon_stock(AddCouponStockRequest $request)
    {
        $params = $request->all();

        isset($params['coupon_id']) && $params['coupon_id'] = (int)$params['coupon_id'];
        isset($params['value']) && $params['value'] = (array)$params['value'];

        $result = $this->service->addCouponStock($params);

        $this->success($result);
    }

    /**
     * 通过核销码获取优惠券
     *
     * @param GetCouponRequest $request
     */
    public function get_coupon(GetCouponRequest $request)
    {
        $params = $request->all();

        $result = $this->service->getCouponByStockValue($params);

        $this->success($result);
    }

    /**
     * 核销优惠券
     *
     * @param CheckCouponStockRequest $request
     */
    public function check_coupon_stock(CheckCouponStockRequest $request)
    {
        $params = $request->all();

        isset($params['coupon_id']) && $params['coupon_id'] = (int)$params['coupon_id'];

        $this->service->checkCoupon($params);

        $this->success();
    }


    /**
     * 添加全店打折信息
     *
     * @param AddShopDiscountRequest $request
     */
    public function add_shop_discount(AddShopDiscountRequest $request)
    {
        $params = $request->all();

        isset($params['title']) && $params['title'] = (string)$params['title'];
        isset($params['picture']) && $params['picture'] = (string)$params['picture'];

        $result = $this->service->AddShopDiscount($params);

        $this->success($result);
    }

    /**
     * 获取全店打折信息
     */
    public function get_shop_discount()
    {
        $result = $this->service->getShopDiscount();

        $this->success($result);
    }


    /**
     * 添加现金抵用券信息
     *
     * @param AddVoucherRequest $request
     */
    public function add_voucher(AddVoucherRequest $request)
    {
        $params = $request->all();

        isset($params['title']) && $params['title'] = (string)$params['title'];
        isset($params['goods_price']) && $params['goods_price'] = (float)$params['goods_price'];
        isset($params['reach_price']) && $params['reach_price'] = (float)$params['reach_price'];
        isset($params['price']) && $params['price'] = (float)$params['price'];
        isset($params['stock']) && $params['stock'] = (int)$params['stock'];
        isset($params['start_time']) && $params['start_time'] = (string)$params['start_time'];
        isset($params['expire_time']) && $params['expire_time'] = (string)$params['expire_time'];
        isset($params['instructions']) && $params['instructions'] = (string)$params['instructions'];

        $result = $this->service->addVoucher($params);

        $this->success($result);
    }


    /**
     * 获取现金抵用券列表
     *
     */
    public function get_enable_voucher_list()
    {

        $result = $this->service->getEnableVoucherList();

        $this->success($result);
    }

    /**
     * 根据id号获取券的具体信息
     *
     * @param GetCouponByIdRequest $request
     */
    public function get_coupon_by_id(GetCouponByIdRequest $request)
    {
        $params = $request->all();

        isset($params['id']) && $params['id'] = (int)$params['id'];
        $result = $this->service->getCouponById($params);

        $this->success($result);
    }

    /**
     * 根据type获取券的具体信息
     *
     * @param GetCouponByTypeRequest $request
     */
    public function get_coupon_by_type(GetCouponByTypeRequest $request)
    {
        $params = $request->all();

        isset($params['type']) && $params['type'] = (int)$params['type'];

        $result = $this->service->getCouponByType($params);

        $this->success($result);
    }

    /**
     * 查找商家id的核销后的券信息
     */
    public function get_used_coupon()
    {
        $status = (int)$this->request->input('status', 1);

        $result = $this->service->getUsedCoupon($status);

        $this->success($result);
    }

    /**
     * 通过订单编号查询订单信息
     *
     * @param GetCouponByOrderNoRequest $request
     */
    public function change_user_coupon_status(GetCouponByOrderNoRequest $request)
    {
        $params = $request->all();
//
        isset($params['order_no']) && $params['order_no'] = (string)$params['order_no'];

        $result = $this->service->changeUserCouponStatus($params);

        $this->success($result);
    }
}